<?php

use yii\db\Migration;

class m180903_125558_user_add_agent_ref extends Migration
{
    public function up()
    {
        $this->addColumn('{{%user}}', 'agent_id', $this->integer()->after('id')->comment('代理商编号'));
        $this->createIndex('fk_user_agent1_idx', '{{%user}}', ['agent_id']);
        try {
            $this->addForeignKey('fk_user_agent1', '{{%user}}', ['agent_id'], '{{%agent}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->addColumn('{{%card}}', 'agent_id', $this->integer()->after('isp_id')->comment('代理商编号'));
        $this->createIndex('fk_card_agent1_idx', '{{%card}}', ['agent_id']);
        try {
            $this->addForeignKey('fk_card_agent1', '{{%card}}', ['agent_id'], '{{%agent}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
    }

    public function down()
    {
        $this->dropForeignKey('fk_card_agent1', '{{%card}}');
        $this->dropIndex('fk_card_agent1_idx', '{{%card}}');
        $this->dropColumn('{{%card}}', 'agent_id');
        $this->dropForeignKey('fk_user_agent1', '{{%user}}');
        $this->dropIndex('fk_user_agent1_idx', '{{%user}}');
        $this->dropColumn('{{%user}}', 'agent_id');
    }
}
